﻿using SD.Infrastructure.EntityBase;
using SD.Toolkits.Recursion.Tree;
using System.Collections.Generic;
using System.Linq;

namespace SD.ContentSystem.Domain.Entities
{
    /// <summary>
    /// 栏目
    /// </summary>
    public class Channel : AggregateRootEntity, ITree<Channel>
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Channel()
        {
            //初始化导航属性
            this.SubNodes = new HashSet<Channel>();
        }
        #endregion

        #region 01.创建栏目构造器
        /// <summary>
        /// 创建栏目构造器
        /// </summary>
        /// <param name="channelNo">栏目编号</param>
        /// <param name="channelName">栏目名称</param>
        /// <param name="storagePath">保存路径</param>
        /// <param name="indexTemplatePath">聚合页模板路径</param>
        /// <param name="listTemplatePath">列表页模板路径</param>
        /// <param name="detailTemplatePath">明细页模板路径</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="sort">排序</param>
        /// <param name="isNavigator">是否是导航</param>
        /// <param name="description">描述</param>
        /// <param name="parentChannel">上级栏目</param>
        public Channel(string channelNo, string channelName, string storagePath, string indexTemplatePath, string listTemplatePath, string detailTemplatePath, int pageSize, int sort, bool isNavigator, string description, Channel parentChannel)
            : this()
        {
            base.Number = channelNo;
            base.Name = channelName;
            this.StoragePath = storagePath;
            this.IndexTemplatePath = indexTemplatePath;
            this.ListTemplatePath = listTemplatePath;
            this.DetailTemplatePath = detailTemplatePath;
            this.PageSize = pageSize;
            this.Sort = sort;
            this.IsNavigator = isNavigator;
            this.Description = description;
            this.ParentNode = parentChannel;
        }
        #endregion

        #endregion

        #region # 属性

        #region 保存路径 —— string StoragePath
        /// <summary>
        /// 保存路径
        /// </summary>
        public string StoragePath { get; private set; }
        #endregion

        #region 聚合页模板路径 —— string IndexTemplatePath
        /// <summary>
        /// 聚合页模板路径
        /// </summary>
        public string IndexTemplatePath { get; private set; }
        #endregion

        #region 列表页模板路径 —— string ListTemplatePath
        /// <summary>
        /// 列表页模板路径
        /// </summary>
        public string ListTemplatePath { get; private set; }
        #endregion

        #region 明细页模板路径 —— string DetailTemplatePath
        /// <summary>
        /// 明细页模板路径
        /// </summary>
        public string DetailTemplatePath { get; private set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        public int PageSize { get; private set; }
        #endregion

        #region 栏目排序 —— int Sort
        /// <summary>
        /// 栏目排序
        /// </summary>
        public int Sort { get; private set; }
        #endregion

        #region 是否是导航 —— bool IsNavigator
        /// <summary>
        /// 是否是导航
        /// </summary>
        public bool IsNavigator { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 链接地址 —— string Url
        /// <summary>
        /// 只读属性 - 链接地址
        /// </summary>
        public string Url
        {
            get
            {
                if (this.IsLeaf)
                {
                    string indexPage = string.Format("{0}/Index.html", this.StoragePath);
                    string listPage = string.Format("{0}/List-1.html", this.StoragePath);

                    if (!string.IsNullOrWhiteSpace(this.IndexTemplatePath))
                    {
                        return indexPage;
                    }
                    if (!string.IsNullOrWhiteSpace(this.ListTemplatePath))
                    {
                        return listPage;
                    }
                }

                return null;
            }
        }
        #endregion

        #region 只读属性 - 是否是根级栏目 —— bool IsRoot
        /// <summary>
        /// 只读属性 - 是否是根级栏目
        /// </summary>
        public bool IsRoot
        {
            get { return this.ParentNode == null; }
        }
        #endregion

        #region 只读属性 - 是否是终级栏目 —— bool IsLeaf
        /// <summary>
        /// 只读属性 - 是否是终级栏目
        /// </summary>
        public bool IsLeaf
        {
            get { return !this.SubNodes.Any(); }
        }
        #endregion

        #region 导航属性 - 上级栏目 —— Channel ParentNode
        /// <summary>
        /// 导航属性 - 上级栏目
        /// </summary>
        public virtual Channel ParentNode { get; private set; }
        #endregion

        #region 导航属性 - 下级栏目集 —— ICollection<Channel> SubNodes
        /// <summary>
        /// 导航属性 - 下级栏目集
        /// </summary>
        public virtual ICollection<Channel> SubNodes { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改栏目信息 —— void UpdateInfo(string channelName, string storagePath...
        /// <summary>
        /// 修改栏目信息
        /// </summary>
        /// <param name="channelName">栏目名称</param>
        /// <param name="storagePath">保存路径</param>
        /// <param name="indexTemplatePath">聚合页模板路径</param>
        /// <param name="listTemplatePath">列表页模板路径</param>
        /// <param name="detailTemplatePath">明细页模板路径</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="sort">排序</param>
        /// <param name="isNavigator">是否是导航</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string channelName, string storagePath, string indexTemplatePath, string listTemplatePath, string detailTemplatePath, int pageSize, int sort, bool isNavigator, string description)
        {
            base.Name = channelName;
            this.StoragePath = storagePath;
            this.IndexTemplatePath = indexTemplatePath;
            this.ListTemplatePath = listTemplatePath;
            this.DetailTemplatePath = detailTemplatePath;
            this.PageSize = pageSize;
            this.Sort = sort;
            this.IsNavigator = isNavigator;
            this.Description = description;
        }
        #endregion

        #endregion
    }
}
